package lock.reentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述：演示多线程预定电影院座位
 */

public class CinemaBookSeat {
    private static Lock lock = new ReentrantLock();
    private static void bookSeat() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "开始预定座位");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "预定座位成功");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        new Thread(() -> bookSeat(), "t1").start();
        new Thread(() -> bookSeat(), "t2").start();
        new Thread(() -> bookSeat(), "t3").start();
        new Thread(() -> bookSeat(), "t4").start();
    }
}
